﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ArtOfTest.WebAii.Controls.HtmlControls;

namespace SM2SetupJobs
{
    public class RunningCompleted
    {
        public static void checkJobStatus(string strJobPartialName)
        {
            Boolean FLAG = true;
            while (FLAG)
            {
                HtmlDiv div_cphPageContent_pnlBlogDetails = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("cphPageContent_pnlBlogDetails");
                HtmlDiv div_cphPageContent_UpdatePanel1 = div_cphPageContent_pnlBlogDetails.Find.ById<HtmlDiv>("cphPageContent_UpdatePanel1");
                HtmlTable tbl_cphPageContent_grdJobStatus = div_cphPageContent_UpdatePanel1.Find.ById<HtmlTable>("cphPageContent_grdJobStatus");

                int intCountRows_Jobs = tbl_cphPageContent_grdJobStatus.AllRows.Count;
                for (int i = 1; i < intCountRows_Jobs; i++)
                {
                    HtmlTableRow tblrow = tbl_cphPageContent_grdJobStatus.AllRows[i];
                    HtmlTableCell tblCellResults = tblrow.Cells[5];

                    IList<HtmlDiv> iListSpan = tblCellResults.Find.AllByAttributes<HtmlDiv>();

                    HtmlDiv divResult = iListSpan[0];


                    string strCellResults = divResult.TextContent;

                    if (strCellResults.Contains(strJobPartialName))
                    {
                        Utilities.GlobalObject.LOGGER.log("Found [" + strJobPartialName + "]");
                        FLAG = false;
                        break;
                    }
                }

                //Reload
                if (FLAG)
                {
                    System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_TIMEOUT_MILISECOND);
                    Utilities.GlobalObject.LOGGER.log("Refresh page ...");
                    Utilities.GlobalObject.MANAGER.ActiveBrowser.Refresh();
                    Utilities.GlobalObject.MANAGER.ActiveBrowser.WaitUntilReady();
                    System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_TIMEOUT_MILISECOND);
                    Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
                }
                else break;
            }
        }

        public static void checkSearchCompleted(string strSearchKeyword)
        {
            System.Threading.Thread.Sleep(60000);
            Boolean FLAG = true;
            Utilities.GlobalObject.LOGGER.log("Refresh page ...");
            Utilities.GlobalObject.MANAGER.ActiveBrowser.Refresh();
            while (FLAG)
            {
                Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
                HtmlDiv div_cphPageContent_pnlBlogDetails = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("cphPageContent_pnlBlogDetails");
                HtmlDiv div_cphPageContent_UpdatePanel1 = div_cphPageContent_pnlBlogDetails.Find.ById<HtmlDiv>("cphPageContent_UpdatePanel1");
                HtmlTable tbl_cphPageContent_grdJobStatus = div_cphPageContent_UpdatePanel1.Find.ById<HtmlTable>("cphPageContent_grdJobStatus");

                try
                {
                    //Check the first row only - this is where the newly created one located
                    HtmlTableRow tblrow = tbl_cphPageContent_grdJobStatus.AllRows[1];
                    HtmlTableCell tblCellProgress = tblrow.Cells[1];

                    IList<HtmlAnchor> iListAnchor = tblCellProgress.Find.AllByAttributes<HtmlAnchor>();

                    HtmlAnchor anchorProgress = iListAnchor[0];

                    SM2Input.Tradition.ClickAnchor(anchorProgress);

                    //Check for expected text              
                    Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
                    HtmlDiv cphPageContent_pnlProgressPopup = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("cphPageContent_pnlProgressPopup");
                    HtmlDiv cphPageContent_pnlLabelsScroller = cphPageContent_pnlProgressPopup.Find.ById<HtmlDiv>("cphPageContent_pnlLabelsScroller");
                    HtmlDiv cphPageContent_divJobProgress = cphPageContent_pnlLabelsScroller.Find.ById<HtmlDiv>("cphPageContent_divJobProgress");
                    string strContent = cphPageContent_divJobProgress.TextContent;

                    if (strContent.Contains("Finished search job"))
                    {
                        FLAG = false;
                        Utilities.GlobalObject.LOGGER.log("Found keyword and it was finished");
                    }

                    //Close the popup if its still waiting for or found
                    HtmlInputSubmit cphPageContent_Button1 = cphPageContent_pnlProgressPopup.Find.ById<HtmlInputSubmit>("cphPageContent_Button1");
                    SM2Input.Tradition.ClickButton(cphPageContent_Button1);
                }
                catch 
                {
                    HtmlTableRow tblrow = tbl_cphPageContent_grdJobStatus.AllRows[2];
                    HtmlTableCell tblCellProgress = tblrow.Cells[1];

                    IList<HtmlAnchor> iListAnchor = tblCellProgress.Find.AllByAttributes<HtmlAnchor>();

                    HtmlAnchor anchorProgress = iListAnchor[0];

                    SM2Input.Tradition.ClickAnchor(anchorProgress);

                    //Check for expected text              
                    Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
                    HtmlDiv cphPageContent_pnlProgressPopup = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("cphPageContent_pnlProgressPopup");
                    HtmlDiv cphPageContent_pnlLabelsScroller = cphPageContent_pnlProgressPopup.Find.ById<HtmlDiv>("cphPageContent_pnlLabelsScroller");
                    HtmlDiv cphPageContent_divJobProgress = cphPageContent_pnlLabelsScroller.Find.ById<HtmlDiv>("cphPageContent_divJobProgress");
                    string strContent = cphPageContent_divJobProgress.TextContent;

                    if (strContent.Contains("Finished search job"))
                    {
                        FLAG = false;
                        Utilities.GlobalObject.LOGGER.log("Found keyword and it was finished");
                    }

                    //Close the popup if its still waiting for or found
                    HtmlInputSubmit cphPageContent_Button1 = cphPageContent_pnlProgressPopup.Find.ById<HtmlInputSubmit>("cphPageContent_Button1");
                    SM2Input.Tradition.ClickButton(cphPageContent_Button1);
                }
                //Reload
                if (FLAG)
                {
                    System.Threading.Thread.Sleep(30000);
                    Utilities.GlobalObject.LOGGER.log("Refresh page ...");
                    Utilities.GlobalObject.MANAGER.ActiveBrowser.Refresh();
                    Utilities.GlobalObject.MANAGER.ActiveBrowser.WaitUntilReady();
                    System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_TIMEOUT_MILISECOND);
                    Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
                }
                else break;
            }
        }
    }
}
